// RecAccountDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VaxVoipSample.h"
#include "AccountRecDlg.h"
#include "afxdialogex.h"
#include "VaxSIPUserAgent.h"

// CAccountRecDlg dialog

IMPLEMENT_DYNAMIC(CAccountRecDlg, CPropertyPage)

CAccountRecDlg::CAccountRecDlg(CVaxSIPUserAgent* pVaxSIPUserAgent): CPropertyPage(CAccountRecDlg::IDD)
{
	m_pVaxSIPUserAgent = pVaxSIPUserAgent;
	
	m_sAccountUserName = "";
	m_sAccountDisplayName = "";
	m_sAccountAuthName = "";
	m_sAccountPassword = "";
	m_sAccountDomainSIP = "";
	m_sAccountProxySIP = "";

	m_objFont.CreateFont(14, 0, 0, 0, FW_BOLD, FALSE, FALSE, 0, ANSI_NULL, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Verdana");
		
	m_bCheckRegisterToProxy = TRUE;
}

CAccountRecDlg::~CAccountRecDlg()
{
}

void CAccountRecDlg::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BTN_ONLINE, m_ctrlInitBtn);

	DDX_Text(pDX, IDC_EDIT_USER_NAME, m_sAccountUserName);
	DDX_Text(pDX, IDC_EDIT_AUTH_LOGIN, m_sAccountAuthName);
	DDX_Text(pDX, IDC_EDIT_DISPLAY_NAME, m_sAccountDisplayName);
	DDX_Text(pDX, IDC_EDIT_AUTH_PASSWORD, m_sAccountPassword);
	DDX_Text(pDX, IDC_EDIT_DOMAIN, m_sAccountDomainSIP);
	DDX_Text(pDX, IDC_EDIT_SIP_PROXY, m_sAccountProxySIP);
	DDX_Check(pDX, IDC_CHECK_REGISTER_SIP_PROXY, m_bCheckRegisterToProxy);
	DDX_Control(pDX, IDC_LIST_STATUSLOG, m_ctrlStatusLogListBox);

	GetDlgItem(IDC_GROUP_SIP_ACCOUNT_REC_SETTING)->SetFont(&m_objFont);
	GetDlgItem(IDC_GROUP_ACCOUNT_REC_STATUS_LOG)->SetFont(&m_objFont);
	GetDlgItem(IDC_GROUP_ACCOUNT_REC_STATUS_LOG2)->SetFont(&m_objFont);

	DDX_Control(pDX, IDC_LIST_CALLLOG, m_ctrlCallStatusLogListBox);
}

BEGIN_MESSAGE_MAP(CAccountRecDlg, CPropertyPage)
	ON_BN_CLICKED(IDC_BTN_ONLINE, &CAccountRecDlg::OnBnClickedBtnOnline)
	ON_BN_CLICKED(IDC_BTN_CALL_LOG_CLEAR, &CAccountRecDlg::OnBnClickedBtnCallLogClear)
	ON_BN_CLICKED(IDC_BTN_STATUS_LOG_CLEAR, &CAccountRecDlg::OnBnClickedBtnStatusLogClear)
END_MESSAGE_MAP()


//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

void CAccountRecDlg::OnBnClickedBtnOnline()
{
	CString sInitBtnText = "";
	m_ctrlInitBtn.GetWindowText(sInitBtnText);

	if(sInitBtnText == "Offline")
	{
		m_pVaxSIPUserAgent->UnRegisterToProxyREC();
		m_ctrlInitBtn.SetWindowText("Online");

		return;
    }
       
    UpdateData(TRUE);

	if(m_sAccountAuthName.IsEmpty())
	{
		MessageBox("Please enter the SIP proxy auth Login.", "SIP Account settings", MB_OK|MB_ICONEXCLAMATION);
		return;
	}

	if(m_sAccountProxySIP.IsEmpty())
	{
		MessageBox("Please enter the SIP proxy address or URI.", "SIP Account settings", MB_OK|MB_ICONEXCLAMATION);
		return;
	}
    
	BOOL bResult = m_pVaxSIPUserAgent->RegisterToProxyREC(m_bCheckRegisterToProxy, 1800, m_sAccountUserName, m_sAccountAuthName, m_sAccountPassword, m_sAccountDisplayName, m_sAccountDomainSIP, m_sAccountProxySIP);

    if(!bResult)
	{
	    return;
	}
	
	if(m_bCheckRegisterToProxy)
		AddToLog("Registering to SIP Server REC.");

	m_ctrlInitBtn.SetWindowText("Offline");
}

void CAccountRecDlg::AddToLog(CString sLog)
{
	m_ctrlStatusLogListBox.AddString(sLog);
}

void CAccountRecDlg::AddToCallLog(CString sLog)
{
	m_ctrlCallStatusLogListBox.AddString(sLog);
}

void CAccountRecDlg::OnBnClickedBtnStatusLogClear()
{
	m_ctrlStatusLogListBox.ResetContent();
}

void CAccountRecDlg::OnBnClickedBtnCallLogClear()
{
	m_ctrlCallStatusLogListBox.ResetContent();
}

void CAccountRecDlg::OnTryingToRegisterREC()
{
	AddToLog("Trying to register.");
}

void CAccountRecDlg::OnFailToRegisterREC(int nStatusCode, LPCTSTR sReasonPhrase)
{
	AddToLog("Failed: " + CString(sReasonPhrase));
}

void CAccountRecDlg::OnSuccessToRegisterREC()
{
	AddToLog("Success to register.");
}

void CAccountRecDlg::OnTryingToReRegisterREC()
{
	AddToLog("Trying to Re-Register.");
}

void CAccountRecDlg::OnFailToReRegisterREC(int nStatusCode, LPCTSTR sReasonPhrase)
{
	AddToLog("Failed: " + CString(sReasonPhrase));
}

void CAccountRecDlg::OnSuccessToReRegisterREC()
{
	 AddToLog("Success to Re-Register.");
}
	
void CAccountRecDlg::OnTryingToUnRegisterREC()
{
	AddToLog("Trying to Unregister.");

}

void CAccountRecDlg::OnFailToUnRegisterREC()
{
	 AddToLog("Fail to unregister.");

}

void CAccountRecDlg::OnSuccessToUnRegisterREC()
{
	 AddToLog("Unregistered successfully.");
}
		
void CAccountRecDlg::OnServerConnectingREC(int nLineNo, int nStatusCode, LPCTSTR sReasonPhrase)
{
	CString sStatus;

	sStatus.Format("Line-%d: Response: %s.", nLineNo + 1, sReasonPhrase);
	AddToCallLog(sStatus);
}

void CAccountRecDlg::OnServerConnectedREC(int nLineNo)
{
	CString sStatus;

	sStatus.Format("Line-%d: Success to connect.", nLineNo + 1);
	AddToCallLog(sStatus);
}

void CAccountRecDlg::OnServerFailedREC(int nLineNo, int nStatusCode, LPCTSTR sReasonPhrase)
{
	CString sStatus;

	sStatus.Format("Line-%d: Response: %s.", nLineNo + 1, sReasonPhrase);
	AddToCallLog(sStatus);
}

void CAccountRecDlg::OnServerHungupREC(int nLineNo)
{
	CString sStatus;

	sStatus.Format("Line-%d: Hang up.", nLineNo + 1);
	AddToCallLog(sStatus);
}

void CAccountRecDlg::OnStatusMsgREC(int nLineNo, DWORD dwErrorCode, CString sErrorMsg)
{
	CString sStatus;

	sStatus.Format("Line-%d: %s.", nLineNo + 1, sErrorMsg);
	AddToCallLog(sStatus);
}





